package com.ebelter.nb.ui.activitys;



import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.ebelter.btcomlib.models.bluetooth.products.nb.BondDevices;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.nb.R;
import com.ebelter.nb.model.http.request.NbNetUtils;
import com.ebelter.nb.model.http.response.NbBandDeviceResponse;
import com.ebelter.nb.utils.NbUtitls;

public class DeviceManagerActivity extends NBBaseActivity implements View.OnClickListener {
    public static final String TAG = "DeviceManagerActivity";
    public static final int RequestCode=77;
    public static final int ResultCode=97;
    private ImageView top_left_iv;
    private TextView title_tv;
    private TextView rightTitle_tv;
    private TextView bpm_sn_tv;
    private TextView scale_sn_tv;

    private TextView jbshb_bpm_tv;
    private TextView jbshb_scale_tv;


    @Override
    protected int loadLayoutById() {
        return R.layout.activity_device_manager;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
        setListener();
        disUpdate();
    }

    private BondDevices isBondBpmDevice;
    private BondDevices isBondScaleDevice;
    private void disUpdate() {

        isBondBpmDevice =  NbUtitls.getBondBpmDevice();
        isBondScaleDevice =  NbUtitls.getBondScaleDevice();

     if (isBondBpmDevice!=null){
         jbshb_bpm_tv.setText("解绑设备");
         bpm_sn_tv.setText(isBondBpmDevice.sn_number);
     }else {
         jbshb_bpm_tv.setText("去绑定");
         bpm_sn_tv.setText("");
     }

        if (isBondScaleDevice!=null){
            jbshb_scale_tv.setText("解绑设备");
            scale_sn_tv.setText(isBondScaleDevice.sn_number);
        }else {
            jbshb_scale_tv.setText("去绑定");
            scale_sn_tv.setText("");
        }
    }

    private void setListener() {
        top_left_iv.setOnClickListener(this);
        jbshb_bpm_tv.setOnClickListener(this);
        jbshb_scale_tv.setOnClickListener(this);
    }

    private void FV() {
        top_left_iv=findViewById(R.id.top_left_iv);
        title_tv=findViewById(R.id.m_title_tv);
        rightTitle_tv=findViewById(R.id.m_righttitle_tv);
        jbshb_bpm_tv=findViewById(R.id.jbshb_bpm_tv);
        jbshb_scale_tv=findViewById(R.id.jbshb_scale_tv);

        bpm_sn_tv=findViewById(R.id.bpm_sn_tv);
        scale_sn_tv=findViewById(R.id.scale_sn_tv);

        ViewUtils.setTextViewStr(title_tv,"设备管理");
        ViewUtils.hideView(rightTitle_tv);
    }

    @Override
    public void onClick(View v) {
        int vid=v.getId();
        if (vid==R.id.top_left_iv){
            finish();
        }else if (vid==R.id.jbshb_bpm_tv){
            jbshb_bpm();
        }else if (vid==R.id.jbshb_scale_tv){
            jbshb_scal();
        }
    }
    private TextTipDialog textTipDialog1;
    private void jbshb_scal() {

        String tip;
        if (isBondScaleDevice!=null){
            tip = "确定要解绑设备吗?";
        }else {
            tip = "请先绑定设备";
        }
        if (textTipDialog1 != null && !textTipDialog1.isShowing()) {
            textTipDialog1.setTip(tip);
            textTipDialog1.show();
        } else {
            textTipDialog1 = new TextTipDialog(this, tip);
            textTipDialog1.setListener(new TextTipDialog.ICancelOkBtListener() {
                @Override
                public void cancel(Dialog dialogView) {
                    dialogView.cancel();
                }
                @Override
                public void ok(Dialog dialogView) {
                    dialogView.cancel();
                    if (isBondScaleDevice!=null){
                        unBondDevice(isBondScaleDevice);
                    }else {
                        Intent addDeviceIntent = new Intent(DeviceManagerActivity.this, AddDeviceActivity.class);
                        startActivityForResult(addDeviceIntent,AddDeviceActivity.requestCode);
                    }
                }
            });
            textTipDialog1.show();

            textTipDialog1.setTextColor(1, 0xff464646);
            textTipDialog1.setTittleDrawableLeft(R.drawable.ic_warring);
            textTipDialog1.setNative_btBg(R.drawable.sp_scccccc_r6);
            textTipDialog1.setPositive_btBg(R.drawable.sp_s0176ff_r6);
        }

        if (isBondScaleDevice!=null){
            textTipDialog1.setPositiveText("确定");
        }else {
            textTipDialog1.setPositiveText("绑定");
        }


    }


    private TextTipDialog textTipDialog;
    private void jbshb_bpm() {

        String tip;
        if (isBondBpmDevice!=null){
            tip = "确定要解绑设备吗?";
        }else {
            tip = "请先绑定设备";
        }
        if (textTipDialog != null && !textTipDialog.isShowing()) {
            textTipDialog.setTip(tip);
            textTipDialog.show();
        } else {
            textTipDialog = new TextTipDialog(this, tip);
            textTipDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
                @Override
                public void cancel(Dialog dialogView) {
                    dialogView.cancel();
                }

                @Override
                public void ok(Dialog dialogView) {
                    dialogView.cancel();
                    if (isBondBpmDevice!=null){
                        unBondDevice(isBondBpmDevice);

                    }else {
                        Intent addDeviceIntent = new Intent(DeviceManagerActivity.this, AddDeviceActivity.class);
                        startActivityForResult(addDeviceIntent,AddDeviceActivity.requestCode);
                    }

                }
            });
            textTipDialog.show();

            if (isBondBpmDevice!=null){
                textTipDialog.setPositiveText("确定");
            }else {
                textTipDialog.setPositiveText("绑定");
            }
            textTipDialog.setTextColor(1, 0xff464646);
            textTipDialog.setTittleDrawableLeft(R.drawable.ic_warring);
            textTipDialog.setNative_btBg(R.drawable.sp_scccccc_r6);
            textTipDialog.setPositive_btBg(R.drawable.sp_s0176ff_r6);
        }

    }

    private void unBondDevice(final BondDevices device) {


        NbNetUtils.getInstance().userUnbandDevice(this, device._id+"", new HttpResponse<NbBandDeviceResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, NbBandDeviceResponse nbBandDeviceResponse, String s) {
                LogUtils.i(TAG,s);
                if (isRequestSuccess && nbBandDeviceResponse!=null && nbBandDeviceResponse.status == 0){
                    NbUtitls.removeBondDevice(device.type);
                    disUpdate();
                    ToastUtil.show("解绑成功");
                }else {
                    ToastUtil.show("解绑失败");
                }
            }
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AddDeviceActivity.requestCode){
            LogUtils.i(TAG,"onActivityResult-AddDeviceActivity.requestCode--重新刷新了一遍");
            disUpdate();
        }
    }

    public void bmpClick(View view) {

        //jbshb_bpm();
    }

    public void scaleClick(View view) {

        //jbshb_scal();
    }
}
